var vm = new Vue({
  el: "#app",
  data: {
    // 当前书籍
    currBook: {},
    // 新约还是旧约
    isNew: true,
    // 新约列表或旧约列表
    currBookList: [],
    // 书籍列表
    bookList,
    // 章节列表
    chapterList,
    count: 1,
  },

  created() {
    if (localStorage.isNew === "false") {
      this.isNew = false;
    } else {
      this.isNew = true;
    }
    var currBookStr = localStorage.currBook;
    var obj = {};
    if (currBookStr) {
      obj = JSON.parse(currBookStr);
    }
    // 新约
    if (this.isNew) {
      if (obj.true) {
        this.currBook = obj.true;
      } else {
        this.currBook = this.newList[0];
      }
      this.currBookList = this.newList;
    } else {
      if (obj.false) {
        this.currBook = obj.false;
      } else {
        this.currBook = this.oldList[0];
      }
      this.currBookList = this.oldList;
    }
  },
  computed: {
    newList() {
      return this.bookList.filter((item) => item.isNew);
    },
    oldList() {
      return this.bookList.filter((item) => !item.isNew);
    },
    currChapterList() {
      return this.chapterList.filter((item) => {
        return item.name === this.currBook.name;
      });
    },
  },

  methods: {
    isdoing(item) { 
        var chapterListInBook = this.chapterList.filter(
          (c) => c.bookId === item.id
        );
        var readedList = chapterListInBook.filter((c) => c.readed);
        if (readedList.length > 0 && readedList.length < item.num) {
          return true;
        } else {
          return false;
        } 
    },

    // 切换新旧约
    changeCurrBookList(num) {
      this.isNew = !this.isNew;
      localStorage.isNew = this.isNew;
      // 新约
      if (num === 1) {
        this.currBookList = this.newList;
      } else {
        this.currBookList = this.oldList;
      }

      var currBookStr = localStorage["currBook"];
      var obj = {};
      if (currBookStr) {
        obj = JSON.parse(currBookStr);
      }

      if (obj[this.isNew]) {
        this.currBook = obj[this.isNew];
      } else {
        this.currBook = this.currBookList[0];
      }
    },

    // 切换书籍
    changeBook(item) {
      this.currBook = item;
      var currBookStr = localStorage.currBook;
      if (currBookStr) {
        var obj = JSON.parse(currBookStr);
      }
      localStorage.currBook = JSON.stringify({
        ...obj,
        [this.isNew]: item,
      });
    },

    // 打卡
    daka(item, index) {
      item.readed = !item.readed;
      // 判断是不是已全部读完该书籍
      var readedList = this.currChapterList.filter((item) => item.readed);
      if (this.currBook.num === readedList.length) {
        this.currBook.finished = true;
      } else {
        this.currBook.finished = false;
      }
      localStorage.chapterList = JSON.stringify(this.chapterList);
      localStorage.bookList = JSON.stringify(this.bookList);
    },
  },
});
